#include "cworkertech.h"

CWorkerTech::CWorkerTech() {}

bool CWorkerTech::removeByCode(QString vCode)
{
    if (vCode.isEmpty())
    {
        return false;
    }
    if (!this->TechByCode.contains(vCode))
    {
        return false;
    }
    CTech * A = TechByCode[vCode];
    if (A == nullptr)
    {
        return false;
    }
    TechByCode.remove(vCode);
    delete A;
    return true;
}

bool CWorkerTech::removeByType(QString vType)
{
    if (vType.isEmpty())
    {
        return false;
    }
    if (!this->TechByType.contains(vType))
    {
        return false;
    }
    CTech * A = TechByType[vType];
    if (A == nullptr)
    {
        return false;
    }
    TechByType.remove(vType);
    delete A;
    return true;
}

bool CWorkerTech::setByCode(QString vCode, float vRate, float * avgRate)
{
    if (vCode.isEmpty())
    {
        return false;
    }
    if (vRate == 0.0)
    {
        return false;
    }
    CTech * p = nullptr;
    if (!this->TechByCode.contains(vCode))
    {
        p= new CTech(vCode, vRate);
        this->TechByCode[vCode] = p;
    }
    else
    {
        p = this->TechByCode[vCode];
        p->update(vRate);
    }
    if (avgRate != nullptr)
    {
        *avgRate = p->fRate;
    }
    return true;
}

bool CWorkerTech::setByType(QString vType, float vRate, float * avgRate)
{
    if (vType.isEmpty())
    {
        return false;
    }
    if (vRate == 0.0)
    {
        return false;
    }
    CTech * p = nullptr;
    if (!this->TechByType.contains(vType))
    {
        p = new CTech(vType, vRate);
        this->TechByType[vType] = p;
    }
    else
    {
        p = this->TechByType[vType];
        p->update(vRate);
    }
    if (avgRate != nullptr)
    {
        *avgRate = p->fRate;
    }
    return true;
}



//没有找到返回0
float CWorkerTech::getByCode(QString vCode)
{
    if (!this->TechByCode.contains(vCode))
    {
        return 0.0;
    }
    CTech * p = nullptr;
    p = this->TechByCode[vCode];
    return p->fRate;
}

//没有找到返回0
float CWorkerTech::getByType(QString vType)
{
    if (!this->TechByType.contains(vType))
    {
        return 0.0;
    }
    CTech * p = nullptr;
    p = this->TechByType[vType];
    return p->fRate;
}
